//
//  GNNetworkingTools.swift
//  SinaWeChat
//
//  Created by mac on 15/12/7.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import AFNetworking

enum GNRequestMethod:String{
    
    case GET = "GET"
    case POST = "POST"
}

class GNNetworkingTools: AFHTTPSessionManager {
    
    
    typealias GNNetworkToolsCallBack = (response: AnyObject?, error: NSError?)->()
    
    static let shareTools:GNNetworkingTools = {
        
        let tools = GNNetworkingTools()
        
        // 在 Swift 里面, 会把 NSSet 转成 Set
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    //发送请求
    
    func request(method:GNRequestMethod,urlString:String,params:AnyObject?,callback:GNNetworkToolsCallBack){
        
        
        // 在 Swift 中,是不能向当前类(AFHTTPSessionManager)发送 dataTaskWithHTTPMethod
        // 因为 dataTaskWithHTTPMethod 这个方法是 AFN 写到 .m 文件里面
        // 那么对应在 Swift 里面就相当于是 private 修饰的方法
        
        //定义成功之后的闭包
        
        let succCallback = {
            (dataTask:NSURLSessionDataTask,response:AnyObject)-> Void in
            
            callback(response: response, error: nil)
            
            
        }
        
        //定义请求失败的闭包
        let failureCallback = {
            (dataTask:NSURLSessionDataTask?,error:NSError) -> Void in
            
            callback(response: nil, error: error)
        }
        
        //根据不同种类请求调用不同的请求方法
        //用自己定义的网络请求类来请求可以避免第三方框架出问题时 ,使得过多的改变app中许多的网络请求
        
        //自定义方法中直接有闭包,里面有成功和失败回调的参数,再在方法里面设置两个失败和成功的回调的闭包,分别在成功和失败的时候调用不一样的参数就行了
        
        
        
        if method == .GET{
            
            self.GET(urlString, parameters: params, success: succCallback, failure: failureCallback)
        }else{
            self.POST(urlString, parameters: params, success: succCallback, failure: failureCallback)
        }
    }

}
//MARK:-首页微博的数据请求

extension GNNetworkingTools{
    
    //因下拉与上啦的要求需要加上两个参数sinceId  maxid
    func loadStatus(accessToken: String,sinceId: Int64 = 0,maxid: Int64 = 0, callback: GNNetworkToolsCallBack){
        
        //请求地址
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        //请求参数
        let params = ["access_token": accessToken,
            "max_id" : "\(maxid)",
            "since_id" : "\(sinceId)"
        ]
        
        request(.GET, urlString: urlString, params: params, callback: callback)
        
    }
    
}

//MARK:-OAuth 登录相关的请求

extension GNNetworkingTools{
    
    
    func loadAccessToken(code:String,callback:GNNetworkToolsCallBack){
        
        //请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数
        let params = [
            
            "client_id" :WB_APPKEY,
            "client_secret": WB_APP_SECRET,
            "grant_type":  "authorization_code",
            "code":code,
            "redirect_uri":WB_REDIRECT_URI
            
        ]
        
        //发送请求
        request(.POST, urlString: urlString, params: params, callback: callback)
    }
    
     func loadUserInfo(uid: String,accessToken: String,callback:GNNetworkToolsCallBack){
        
        
        //定义url
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //组织参数
        let params = [
            "access_token":accessToken,
            "uid":uid
        ]
        
        request(.GET, urlString: urlString, params: params, callback: callback)
    }
}

